package cn.iocoder.yudao.module.vos.controller.admin.tenantrequestlog;

import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.vos.controller.admin.tenantrequestlog.vo.*;
import cn.iocoder.yudao.module.vos.dal.dataobject.tenantrequestlog.TenantRequestLogDO;
import cn.iocoder.yudao.module.vos.service.tenantrequestlog.TenantRequestLogService;

@Tag(name = "管理后台 - 租户请求日志")
@RestController
@RequestMapping("/vos/tenant-request-log")
@Validated
@TenantIgnore
public class TenantRequestLogController {

    @Resource
    private TenantRequestLogService tenantRequestLogService;

    @PostMapping("/create")
    @Operation(summary = "创建租户请求日志")
    @PreAuthorize("@ss.hasPermission('vos:tenant-request-log:create')")
    public CommonResult<Long> createTenantRequestLog(@Valid @RequestBody TenantRequestLogSaveReqVO createReqVO) {
        return success(tenantRequestLogService.createTenantRequestLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新租户请求日志")
    @PreAuthorize("@ss.hasPermission('vos:tenant-request-log:update')")
    public CommonResult<Boolean> updateTenantRequestLog(@Valid @RequestBody TenantRequestLogSaveReqVO updateReqVO) {
        tenantRequestLogService.updateTenantRequestLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除租户请求日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vos:tenant-request-log:delete')")
    public CommonResult<Boolean> deleteTenantRequestLog(@RequestParam("id") Long id) {
        tenantRequestLogService.deleteTenantRequestLog(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除租户请求日志")
                @PreAuthorize("@ss.hasPermission('vos:tenant-request-log:delete')")
    public CommonResult<Boolean> deleteTenantRequestLogList(@RequestParam("ids") List<Long> ids) {
        tenantRequestLogService.deleteTenantRequestLogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得租户请求日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vos:tenant-request-log:query')")
    public CommonResult<TenantRequestLogRespVO> getTenantRequestLog(@RequestParam("id") Long id) {
        TenantRequestLogDO tenantRequestLog = tenantRequestLogService.getTenantRequestLog(id);
        return success(BeanUtils.toBean(tenantRequestLog, TenantRequestLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得租户请求日志分页")
    @PreAuthorize("@ss.hasPermission('vos:tenant-request-log:query')")
    public CommonResult<PageResult<TenantRequestLogRespVO>> getTenantRequestLogPage(@Valid TenantRequestLogPageReqVO pageReqVO) {
        PageResult<TenantRequestLogDO> pageResult = tenantRequestLogService.getTenantRequestLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TenantRequestLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出租户请求日志 Excel")
    @PreAuthorize("@ss.hasPermission('vos:tenant-request-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTenantRequestLogExcel(@Valid TenantRequestLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TenantRequestLogDO> list = tenantRequestLogService.getTenantRequestLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "租户请求日志.xls", "数据", TenantRequestLogRespVO.class,
                        BeanUtils.toBean(list, TenantRequestLogRespVO.class));
    }

    // 按月导出账单（.xlsx）
    @GetMapping("/export-bill")
    @Operation(summary = "导出租户月度账单（按 request_type + secret_id 聚合）")
    @Parameter(name = "yearMonth", description = "年月，格式 yyyy-MM，例如 2025-10", required = true)
    @PreAuthorize("@ss.hasPermission('vos:tenant-request-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMonthlyBill(@RequestParam("yearMonth") String yearMonth,
                                  HttpServletResponse response) throws IOException {
        List<TenantRequestMonthlyBillRow> rows = tenantRequestLogService.exportMonthlyBill(yearMonth);
        String fileName = "租户请求账单-" + yearMonth + ".xlsx";
        ExcelUtils.write(response, fileName, "账单", TenantRequestMonthlyBillRow.class, rows);
    }

}